#include <iostream>
#include <vector>

using namespace std;

int m, n;
vector<vector<int>> map;

int main()
{
    cin >> m >> n;
    for (int i = 0; i < m; i++)
    {
        vector<int> temp;
        for (int j = 0; j < n; j++)
        {
            int k;
            cin >> k;
            temp.push_back(k);
        }
        map.push_back(temp);
    }

    vector<int> ans;
    int u = 0; 
    int d = m - 1;
    int l = 0;
    int r = n - 1;
    while (true)
    {
        for (int i = l; i <= r; ++i) ans.push_back(map[u][i]);
        if (++u > d) break;
        for (int i = u; i <= d; ++i) ans.push_back(map[i][r]);
        if (--r < l) break;
        for (int i = r; i >= l; --i) ans.push_back(map[d][i]);
        if (--d < u) break;
        for (int i = d; i >= u; --i) ans.push_back(map[i][l]);
        if (++l > r) break;
    }

    for(int i=0;i<ans.size()-1;i++)
    {
        cout<<ans[i]<<" ";
    }
    cout<<ans[ans.size()-1]<<endl;
    return 0;
}